/**
 *WeiboAdapter.java
 *2011-9-29 下午08:18:26
 *Touch Android
 *http://bbs.droidstouch.com
 */
package com.krui.iweibo.adapter;

import java.util.Date;
import java.util.List;

import weibo4android.Status;
import android.R.integer;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.krui.iweibo.R;
import com.krui.iweibo.app.IweiboApplication;
import com.krui.iweibo.service.imageprocess.ImageLoaderCallback;
import com.krui.iweibo.service.imageprocess.SimpleImageLoader;
import com.krui.iweibo.service.parseweibo.SimpleWeiboManager;
import com.krui.iweibo.utils.StringUtil;

/**
 * 
 * @author krui
 *
 */
@SuppressLint({ "ParserError", "ParserError" })
public class WeiboAdapter extends BaseAdapter
{

	
	private List<Status> status;
	private Context context;
	
	private LayoutInflater mInflater;
	
	
	
	public WeiboAdapter(Context context,List<Status> status) 
	{
		
		this.status = status;
		this.context = context;
		mInflater = LayoutInflater.from(context);
	}
	
	
	public int getCount()
	{
		return status==null?0:status.size();
	}

	public Object getItem(int position)
	{
		return  status==null?null:status.get(position);
	}

	public long getItemId(int position)
	{
		return status.get(position).getId();
	}

	@SuppressLint("ParserError")
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		
		return CreateViewFromResource(position, convertView);
	}
	
	
	private View CreateViewFromResource(int position,View convertView){
		View v;
		if (convertView == null) {
			v = mInflater.inflate(R.layout.wb_item_template, null);
		}else {
			v = convertView;
		}
		
		bindView(position,v);
		return v;
	}

	
	private void bindView(int position,View view){

		WeiboHolder holder = null;
		Status s = this.status.get(position);
		holder = new WeiboHolder();
		
		
		
		holder.txt_wb_item_uname = (TextView) view.findViewById(R.id.txt_wb_item_uname);
		holder.txt_wb_item_content= (TextView) view.findViewById(R.id.txt_wb_item_content);
		holder.txt_wb_item_time = (TextView)view.findViewById(R.id.txt_wb_item_time);
		holder.txt_wb_item_from= (TextView) view.findViewById(R.id.txt_wb_item_from);
		holder.txt_wb_item_uname.setText(s.getUser().getName());
		
		//SimpleWeiboManager.parser(holder.txt_wb_item_uname, s.getText());
		SimpleWeiboManager.parser(holder.txt_wb_item_content, s.getText());
		
		holder.txt_wb_item_time.setText(convertTime(s.getCreatedAt()));
		
		holder.img_wb_item_head = (ImageView)view.findViewById(R.id.img_wb_item_head);
		holder.img_wb_item_head_v = (ImageView)view.findViewById(R.id.img_wb_item_head_v);
		holder.img_wb_item_content_pic = (ImageView) view.findViewById(R.id.img_wb_item_content_pic);
		holder.lyt_wb_item_sublayout = (LinearLayout) view.findViewById(R.id.lyt_wb_item_sublayout);
		holder.txt_wb_item_subcontent = (TextView) view.findViewById(R.id.txt_wb_item_subcontent);
		holder.img_wb_item_content_subpic = (ImageView) view.findViewById(R.id.img_wb_item_content_subpic);
		
		SimpleImageLoader.showImg(holder.img_wb_item_head , s.getUser().getProfileImageURL().toString());
		holder.txt_wb_item_from.setText("来自:"+Html.fromHtml(s.getSource()));
		
		
		//判断是否通过认证
		if(s.getUser().isVerified())
		{
			holder.img_wb_item_head_v.setVisibility(View.VISIBLE);
		}else {
			holder.img_wb_item_head_v.setVisibility(View.GONE);
		}
		
		//判断微博中是否含有图片
		if(!StringUtil.isEmpty(s.getThumbnail_pic()))
		{
			
			holder.img_wb_item_content_pic.setVisibility(View.VISIBLE);
			SimpleImageLoader.showImg(holder.img_wb_item_content_pic, s.getThumbnail_pic());
		}else {
			holder.img_wb_item_content_pic.setVisibility(View.GONE);
		}
		
		// 判断是否有转发
		if(s.getRetweeted_status()!=null)
		{
			
			holder.lyt_wb_item_sublayout.setVisibility(View.VISIBLE);
			
			String weiboText = "@" + s.getRetweeted_status().getUser().getName() + ":" +s.getRetweeted_status().getText();
			SimpleWeiboManager.parser(holder.txt_wb_item_subcontent, weiboText);
			
			
			//holder.txt_wb_item_subcontent.setText("@" + s.getRetweeted_status().getUser().getName() + ":" +s.getRetweeted_status().getText());
			
			//判断微博中是否含有图片
			if(!StringUtil.isEmpty(s.getRetweeted_status().getThumbnail_pic()))
			{
				holder.img_wb_item_content_subpic.setVisibility(View.VISIBLE);
				SimpleImageLoader.showImg(holder.img_wb_item_content_subpic, s.getRetweeted_status().getThumbnail_pic());
			}else {
				holder.img_wb_item_content_subpic.setVisibility(View.GONE);
			}
			
		}else {
			holder.lyt_wb_item_sublayout.setVisibility(View.GONE);
		}
	
	}
	
	
	
	private String convertTime(Date date) {
		String str = null;
		Date now = new Date();
		long l = now.getTime() - date.getTime();
		long day = l / (24 * 60 * 60 * 1000);
		long hour = (l / (60 * 60 * 1000) - day * 24);
		long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
		long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		if (day != 0) {
			str = String.valueOf(l) + "天前";
		} else if (day < 0 && hour > 0) {
			str = String.valueOf(hour) + "小时前";
		} else if (hour < 0 && min > 0) {
			str = String.valueOf(min) + "分钟前";
		} else {
			str = String.valueOf(s) + "秒前";
		}
		return str;
	}
	
	private static class WeiboHolder
	{
		
		ImageView img_wb_item_head;
		
		ImageView img_wb_item_head_v;
		
		TextView txt_wb_item_uname;
		
		
		TextView txt_wb_item_time;
		
		TextView txt_wb_item_content;
		
		ImageView img_wb_item_content_pic;
		
		LinearLayout lyt_wb_item_sublayout;
		
		TextView txt_wb_item_subcontent;
		
		ImageView img_wb_item_content_subpic;
		
		TextView txt_wb_item_from;
		
		TextView txt_wb_item_redirect;
		
		TextView txt_wb_item_comment;
		
	}
	
	
	
}
